package engine.sound
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.event.SceneEvent;
	import engine.event.SoundEvent;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	
	import newx.com.config.StaticIni;
	import newx.com.loader.SwfLoader;

	public class SoundManage
	{
		private static var self:SoundManage;
		private var model:ModelLocator;
		
		private var bgVolume:Number	=	0.6;
		
		private var soundData:Object;
		public var musicState:Boolean		=	true;
		
		private var bgSound:Sound;
		private var bgChannel:SoundChannel;
		
		private var effectChannel:SoundChannel;
		
		private var isEffectPlay:Boolean	=	false;
		
		public static const ADDITION:String	=	"addition";
		public static const SINGLE:String		=	"single";
		
		private var slowTime:int				=	3;
		
		public function SoundManage(val:singleSample)
		{
			init();
		}
		
		public static function getInstance():SoundManage
		{
			if(self==null)
			{
				self	=	new SoundManage(new singleSample);
			}
			return self;
		}
		
		private function init():void
		{
			model		=	ModelLocator.getInstance();
			addEvents();
			var userdata:SharedObject	=	SharedObject.getLocal("userdata");
			if(userdata.data.musicGroupIndex)
			{
				musicState	=	userdata.data.musicGroupIndex == 0 ? true : false;
			}
		}
		
		private function addEvents():void
		{
			model.addEventListener(SoundEvent.MUSIC_ON,onMusicOnOrOff);
			model.addEventListener(SoundEvent.MUSIC_OFF,onMusicOnOrOff);
			
			model.addEventListener(SceneEvent.SceneLoaded,onSceneLoaded);
			
			model.addEventListener(SoundEvent.UI_SHOW,onEffectPlay);
			model.addEventListener(SoundEvent.UI_HIDE,onEffectPlay);
			model.addEventListener(SoundEvent.LEVEL_UP,onEffectPlay);
			model.addEventListener(SoundEvent.PACKET_SORT,onEffectPlay);
			model.addEventListener(SoundEvent.EQUIP_LEVEL_UP,onEffectPlay);
			model.addEventListener(SoundEvent.EQUIP_UP,onEffectPlay);
			model.addEventListener(SoundEvent.EQUIP_DOWN,onEffectPlay);
			model.addEventListener(SoundEvent.SWALLOW_MEDICINE,onEffectPlay);
			model.addEventListener(SoundEvent.KUNGFU_BEST,onEffectPlay);
			model.addEventListener(SoundEvent.SKILL_NEW,onEffectPlay);
			model.addEventListener(SoundEvent.INDUSTRY_MONEY,onEffectPlay);
			model.addEventListener(SoundEvent.ESCORT_CALL,onEffectPlay);
			model.addEventListener(SoundEvent.BATTLE_SUM,onEffectPlay);
			model.addEventListener(SoundEvent.OPEN_BOX,onEffectPlay);
			model.addEventListener(SoundEvent.ROUND_NUM,onEffectPlay);
			model.addEventListener(SoundEvent.TURN_CARD,onEffectPlay);
			model.addEventListener(SoundEvent.TURN_ROUND_CARD,onEffectPlay);
			model.addEventListener(SoundEvent.CIMELIA_GOT,onEffectPlay);
			model.addEventListener(SoundEvent.COMBINE_ITEMS,onEffectPlay);
			model.addEventListener(SoundEvent.MOUSE_OVER,onEffectPlay);
//			model.addEventListener(SoundEvent.MOUSE_OUT,onEffectPlay);
		}
		
		private function onEffectPlay(e:SoundEvent):void
		{
			if(!musicState) return;
			var effectType:String	=	e.type;
			switch(effectType)
			{
				case SoundEvent.UI_SHOW:
					playEffect(getURL(SoundEvent.UI_SHOW),SINGLE);
					break;
				case SoundEvent.UI_HIDE:
					playEffect(getURL(SoundEvent.UI_HIDE),SINGLE);
					break;
				case SoundEvent.LEVEL_UP:
					playEffect(getURL(SoundEvent.LEVEL_UP));
					break;
				case SoundEvent.PACKET_SORT:
					playEffect(getURL(SoundEvent.PACKET_SORT));
					break;
				case SoundEvent.EQUIP_LEVEL_UP:
					playEffect(getURL(SoundEvent.EQUIP_LEVEL_UP));
					break;
				case SoundEvent.EQUIP_UP:
					playEffect(getURL(SoundEvent.EQUIP_UP));
					break;
				case SoundEvent.EQUIP_DOWN:
					playEffect(getURL(SoundEvent.EQUIP_DOWN));
					break;
				case SoundEvent.SWALLOW_MEDICINE:
					playEffect(getURL(SoundEvent.SWALLOW_MEDICINE));
					break;
				case SoundEvent.KUNGFU_BEST:
					playEffect(getURL(SoundEvent.KUNGFU_BEST));
					break;
				case SoundEvent.SKILL_NEW:
					playEffect(getURL(SoundEvent.SKILL_NEW));
					break;
				case SoundEvent.INDUSTRY_MONEY:
					playEffect(getURL(SoundEvent.INDUSTRY_MONEY));
					break;
				case SoundEvent.ESCORT_CALL:
					playEffect(getURL(SoundEvent.ESCORT_CALL));
					break;
				case SoundEvent.BATTLE_SUM:
					playEffect(getURL(SoundEvent.BATTLE_SUM));
					break;
				case SoundEvent.OPEN_BOX:
					playEffect(getURL(SoundEvent.OPEN_BOX));
					break;
				case SoundEvent.ROUND_NUM:
					playEffect(getURL(SoundEvent.ROUND_NUM));
					break;
				case SoundEvent.TURN_CARD:
					playEffect(getURL(SoundEvent.TURN_CARD));
					break;
				case SoundEvent.TURN_ROUND_CARD:
					playEffect(getURL(SoundEvent.TURN_ROUND_CARD));
					break;
				case SoundEvent.CIMELIA_GOT:
					playEffect(getURL(SoundEvent.CIMELIA_GOT));
					break;
				case SoundEvent.COMBINE_ITEMS:
					playEffect(getURL(SoundEvent.COMBINE_ITEMS));
					break;
				case SoundEvent.MOUSE_OVER:
					playEffect(getURL(SoundEvent.MOUSE_OVER));
					break;
				case SoundEvent.MOUSE_OUT:
					playEffect(getURL(SoundEvent.MOUSE_OUT));
					break;
			}
		}
		
		private function onMusicOnOrOff(e:SoundEvent):void
		{
			if(e.type==SoundEvent.MUSIC_ON)
			{
				musicState		=	true;
				playBG();
			}
			else
			{
				musicState		=	false;
				stopBG();
			}
		}
		
		private function onSceneLoaded(e:SceneEvent):void
		{
			if(soundData==null) soundData	=	StaticIni.getIniObj("Sound");
			bgVolume						=	Number(soundData["volume"]);
			var sceneConfig:int		=	GameData.SceneConfig;
			setTimeout(playBGMusic,slowTime*1000,[sceneConfig]);
		}
		
		private function playBGMusic(sceneID:int):void
		{
			var nameID:String			=	"scene_" + sceneID;
			var sceneConfig:String		=	nameID.slice(0,-2);
			playMusic(getURL(sceneConfig));
		}
		
		/**
		 * @param url getURL()后的地址
		 */		
		public function playMusic(url:String):void
		{
			if(url=="") 	return;
			if(bgChannel!=null) bgChannel.stop();
			
			bgSound						=	new Sound();
			
			bgSound.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void
			{
//				trace(url + "->url配置路径问题");
			}
			);
			
			bgSound.load(new URLRequest(url));
			bgChannel					=	bgSound.play(0,9999999);
			
			if(musicState)
			{
				playBG();
			}
			else
			{
				stopBG();
			}
		}
		
		/**
		 * @param url getURL()后的地址
		 * @param type 播放类型
		 */		
		public function playEffect(url:String,type:String=ADDITION):void
		{
			if(url=="") return;
			if(type==SoundManage.SINGLE && isEffectPlay) return;
			
			var sound:Sound		=	new Sound();
			sound.addEventListener(Event.COMPLETE,onEffectOver);
			
			sound.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void
			{
//				trace(url + "->url配置路径问题");
			}
			);
			
			sound.load(new URLRequest(url));
			
			effectChannel	=	sound.play();
			var soundTrans:SoundTransform 	= new SoundTransform();
			soundTrans.volume 				= bgVolume;
			if(soundTrans !=null )
			{
				effectChannel.soundTransform	=	soundTrans;
			}
			isEffectPlay	=	true;
		}
		
		private function onEffectOver(e:Event):void
		{
			isEffectPlay	=	false;
			(e.target as Sound).removeEventListener(Event.COMPLETE,onEffectOver);
		}
		
		/**
		 * @param configName 外部配置参数
		 * @return 
		 */		
		private function getURL(configName:String):String
		{
			if(soundData==null) return "";
			if(soundData.hasOwnProperty(configName))
			{
				return SwfLoader.CDN + soundData[configName];
			}
			else
			{
				return "";
			}
		}
		
		private function playBG(isSlow:Boolean=false):void
		{
			if(bgChannel) 
			{
				setVolume(bgChannel,0);
				setVolume(bgChannel,bgVolume,isSlow);
			}
		}
		
		public function stopBG(isSlow:Boolean=false):void
		{
			if(bgChannel==null) return;
			setVolume(bgChannel,0,isSlow);
		}
		
		private function setVolume(channel:SoundChannel,volume:Number,isSlow:Boolean=false):void
		{
			if(!isSlow)
			{
				var soundTrans:SoundTransform 		= new SoundTransform();
				soundTrans.volume	 				= volume;
				channel.soundTransform 			= soundTrans;
			}
			else
			{
				var trans:SoundTransform	=	bgChannel.soundTransform;
				TweenLite.to(trans,slowTime,{volume:volume,ease:Linear.easeNone,onUpdate:function():void{channel.soundTransform=trans}});
			}
		}
		
		
	}
}


class singleSample{}